package artid.com.arttogo.view.horizonlistview;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.TextView;

import com.android.volley.VolleyError;

import java.util.ArrayList;
import java.util.List;

import artid.com.arttogo.R;
import artid.com.arttogo.activity.BaseActivity;
import artid.com.arttogo.db.Cache;
import artid.com.arttogo.network.VolleyBasic;
import artid.com.arttogo.network.VolleyCallback;
import artid.com.arttogo.network.VolleyData;
import artid.com.arttogo.network.VolleyMap;
import artid.com.arttogo.view.MyListView;
import artid.com.arttogo.view.pulllist.PullToRefreshLayout;

/**
 * 
 *    
 * 项目名称：ArtworkArt   
 * 类名称：HorizontalListView_Activity   
 * 类描述：暂时用于显示竖向ListView嵌套横向ListView
 * 创建人：耿卫斌   
 * 创建时间：2017-1-4 上午10:56:48   
 *
 */
public class HorizontalListView_Activity extends BaseActivity {
	private Context mContext;
	private MyListView mListView;
	private PullToRefreshLayout mPullLayout;
	private boolean isRefreshed = false;// 是否刷新
	private int page=1;
	private List<NewartistArtistListParser.NewartistArtistList_ArtistList> list;
	private List<NewartistArtistListParser.NewartistArtistList_ArtistList> more_list;
	private NewartistArtistList_Adapter adapter_ListView;
	private PopupWindow window;
	//请求类别
	private String ac_id="1";
	//请求排序
	private String orderid="1";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_horizontallistview);
		initView();
	}


	private static List<ImageView> dotViewList;
	ArrayList<String> imgFocusList, urlFocusList;
	private void initView(){
		mContext=HorizontalListView_Activity.this;
		list=new ArrayList<NewartistArtistListParser.NewartistArtistList_ArtistList>();
		more_list=new ArrayList<NewartistArtistListParser.NewartistArtistList_ArtistList>();
		mListView=(MyListView) findViewById(R.id.ListViewAndHorizonListview_ListView);
		mPullLayout=(PullToRefreshLayout) findViewById(R.id.refresh_layout);
		mPullLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {

			@Override
			public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
				// TODO Auto-generated method stub
				//下拉刷新
				page=1;
				isRefreshed=true;
				getArtistListData();
			}

			@Override
			public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
				// TODO Auto-generated method stub
				// pullToRefreshLayout.setBottomDismiss();
				//上拉加载
				page++;
				getArtistListData();
			}
		}); 
	}

	/**
	 * 获取艺术家信息和作品信息。
	 * 页数	page	
		条数	pagesize	
		艺术家类别	ac_id	
		口令	token	
		排序规则	orderid	默认的排序规则是降序
		默认排序：1；
		按人气从高到底：2
		按加入顺序倒序：3
		按作品数从多到少：4
		按点赞数从高到底：5
		按姓氏从A-Z：6
	 */
	private void getArtistListData(){
		VolleyBasic.getInstance(this).post(Cache.Newartist_artistList,
				VolleyMap.getInstance().Newartist_artistList(page, 1, ac_id, orderid),
				NewartistArtistListParser.class, new VolleyCallback() {

			@Override
			public void onSuccess(VolleyData vd) {
				// TODO Auto-generated method stub
				mPullLayout.loadmoreFinish(mPullLayout.SUCCEED);
				NewartistArtistListParser mBean=new NewartistArtistListParser();
				mBean=(NewartistArtistListParser) vd.getData();
				list=mBean.getArtistList();
				if (isRefreshed == true) {
					more_list.clear();
					isRefreshed = false;
				}
				if (!list.isEmpty()) {
					more_list.addAll(list);
				}
				/*
				//这里是显示有数据和无数据的布局
				if(more_list!=null && more_list.size()!=0){
					noDataLL.setVisibility(View.GONE);
				}else{
					noDataLL.setVisibility(View.VISIBLE);
					return;
				}
				 */
				if (adapter_ListView == null) {
					adapter_ListView=new NewartistArtistList_Adapter(mContext, more_list,false);
					mListView.setAdapter(adapter_ListView);
				}
				adapter_ListView.notifyDataSetChanged();
				Log.d("log", more_list.toString());
			}

			@Override
			public void onError(VolleyError volleyError) {
				// TODO Auto-generated method stub
				mPullLayout.loadmoreFinish(mPullLayout.FAIL);
			}
			});
	}

public  void showPopwindow(final Context mContext, View v, int ViewId) {
	// 利用layoutInflater获得View
	LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	View view = inflater.inflate(R.layout.popupwindow_artistlistsort, null);
	// 下面是两种方法得到宽度和高度 getWindow().getDecorView().getWidth()
	window = new PopupWindow(view,
			WindowManager.LayoutParams.MATCH_PARENT,
			WindowManager.LayoutParams.WRAP_CONTENT);
	// 设置popWindow弹出窗体可点击，这句话必须添加，并且是true
	window.setFocusable(true);
	window = new PopupWindow(view, WindowManager.LayoutParams.FILL_PARENT, WindowManager.LayoutParams.WRAP_CONTENT,false);
	//设置可以获取焦点，否则弹出菜单中的EditText是无法获取输入的
	window.setFocusable(true);
	//这句是为了防止弹出菜单获取焦点之后，点击activity的其他组件没有响应
	window.setBackgroundDrawable(new BitmapDrawable());
	// 设置popWindow的显示和消失动画
	window.setAnimationStyle(R.style.AnimBottom);
	//防止虚拟软键盘被弹出菜单遮住
	window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
	// 在底部显示
	window.showAtLocation(v.findViewById(ViewId), Gravity.BOTTOM, 0, 0);
	// 这里检验popWindow里的button是否可以点击
	TextView defaultBtn = (TextView) view.findViewById(R.id.popupwindow_artistlistsort_default);
	defaultBtn.setOnClickListener(new OnClickListener() {
		@Override
		public void onClick(View v) {
			//默认排序
			orderid="1";
			page=1;
			isRefreshed=true;
			getArtistListData();
			window.dismiss();
		}
	});
	TextView popularityBtn = (TextView) view.findViewById(R.id.popupwindow_artistlistsort_popularity);
	popularityBtn.setOnClickListener(new OnClickListener() {
		@Override
		public void onClick(View v) {
			//按人气从高到低
			orderid="2";
			page=1;
			isRefreshed=true;
			getArtistListData();
			window.dismiss();
		}
	});
	TextView joinBtn = (TextView) view.findViewById(R.id.popupwindow_artistlistsort_join);
	joinBtn.setOnClickListener(new OnClickListener() {
		@Override
		public void onClick(View v) {
			//按加入顺序倒序
			orderid="3";
			page=1;
			isRefreshed=true;
			getArtistListData();
			window.dismiss();
		}
	});
	TextView worksNumBtn = (TextView) view.findViewById(R.id.popupwindow_artistlistsort_worksNum);
	worksNumBtn.setOnClickListener(new OnClickListener() {
		@Override
		public void onClick(View v) {
			//按作品数从多到少
			orderid="4";
			page=1;
			isRefreshed=true;
			getArtistListData();
			window.dismiss();
		}
	});
	TextView giveTheThumbsUpBtn = (TextView) view.findViewById(R.id.popupwindow_artistlistsort_giveTheThumbsUp);
	giveTheThumbsUpBtn.setOnClickListener(new OnClickListener() {
		@Override
		public void onClick(View v) {
			//按点赞数从高到低
			orderid="5";
			page=1;
			isRefreshed=true;
			getArtistListData();
			window.dismiss();
		}
	});
	TextView surnameBtn = (TextView) view.findViewById(R.id.popupwindow_artistlistsort_surname);
	surnameBtn.setOnClickListener(new OnClickListener() {
		@Override
		public void onClick(View v) {
			//按姓氏从A-Z
			orderid="6";
			page=1;
			isRefreshed=true;
			getArtistListData();
			window.dismiss();
		}
	});
	TextView cancelBtn = (TextView) view.findViewById(R.id.popupwindow_artistlistsort_cancel);
	cancelBtn.setOnClickListener(new OnClickListener() {
		@Override
		public void onClick(View v) {
			//cancelBtn
			window.dismiss();
		}
	});
	//popWindow消失监听方法
	window.setOnDismissListener(new OnDismissListener() {

		@Override
		public void onDismiss() {
			System.out.println("popWindow消失");
		}
	});
}
}
